package com.ts.mediacodec.Activity

import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.ts.audiocamera.R
import com.ts.camera2.Camera2Wrapper
import com.ts.camera2.camera.Camera2FrameCallback
import com.ts.mediacodec.H264Decode

class MediaCodecActivity : AppCompatActivity(), SurfaceHolder.Callback, Camera2FrameCallback {
    var h264Encode: H264Decode? = null
    var mCamera2Wrapper:Camera2Wrapper?=null
    var mSurfaceview:SurfaceView?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_media_codec)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        mSurfaceview=findViewById(R.id.surface_media_codec);

        mSurfaceview?.holder?.addCallback(this)
        mCamera2Wrapper=Camera2Wrapper(this);
    }

    override fun surfaceCreated(holder: SurfaceHolder) {

    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        mCamera2Wrapper?.startCamera(holder.surface)
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {

    }

    private  val TAG = "MediaCodecActivity"
    override fun onPreviewFrame(data: ByteArray?, width: Int, height: Int) {
        Log.e(TAG,"onPre")
        if (h264Encode!=null){
            h264Encode=H264Decode(width,height);
        }

        data?.let { h264Encode?.encodeFrame(it) }
    }

    override fun onCaptureFrame(data: ByteArray?, width: Int, height: Int) {

    }
}